package com.fc.incross_client.engine;

import org.omg.CORBA.Context;
import org.omg.CORBA.ServerRequest;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: GY.
 * @Description: TODO()
 * @Date:Created in 2020/2/24 0024.
 * @Modified By:
 */
@Component
public class Client implements InitializingBean {
    @Autowired
    private RestTemplate restTemplate;
    private static ExecutorService executorService = Executors.newSingleThreadExecutor();
    //    @Value("incross.server.port")
    private static int port = 10001;
    //    @Value("incross.server.ip")
    private static String serverHost = "127.0.0.1";


    private static class ClientStartTask implements Runnable {

        @Override
        public void run() {
            Socket socket = null;
            try {
                socket = new Socket(serverHost, port);
            } catch (IOException e) {
                e.printStackTrace();
            }
            OutputStream outputStream = null;
            try {
                outputStream = socket.getOutputStream();
            } catch (IOException e) {
                e.printStackTrace();
            }

            String code = "I'm Client";
            byte[] bytes = new byte[0];
            try {
                bytes = code.getBytes("utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            try {
                outputStream.write(bytes);
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

            while (true) {
                InputStream inputStream = null;
                try {
                    inputStream = socket.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                byte[] requestByte = new byte[4096];
                try {
                    inputStream.read(requestByte);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    System.out.println("Server's Request is =================== " + new String(requestByte, "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                try {
                    String s = StreamUtils.copyToString(inputStream, Charset.forName("UTF-8"));
                    String trim = s.trim();
                    requestByte = trim.getBytes();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // 代理调用 restTemplate TODO
                code = "OK";
//                restTemplate.
                try {
                    bytes = code.getBytes("utf-8");
                    outputStream.write(bytes);
                    outputStream.flush();
                    System.out.println("Response success");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        public void executor() {
            executorService.execute(this);
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=================Client==================");
        new ClientStartTask().executor();
    }
}
